package link.cocoon.blog.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import link.cocoon.common.base.BaseService;
import link.cocoon.blog.entity.Category;
import link.cocoon.blog.mapper.CategoryMapper;
import link.cocoon.blog.service.CategoryService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Cocoon
 * @since 2020-03-31
 */
@Service
public class CategoryServiceImpl extends BaseService<CategoryMapper, Category> implements CategoryService {

  @Override
  public List<Long> findAllChildrenIdById(Long id) {
    List<Long> childrenIds = new ArrayList<>();
    childrenIds.add(id);
    traverseAllChildren(Collections.singletonList(id),childrenIds);
    return childrenIds;
  }

  private void traverseAllChildren(List<Long> ids, List<Long> childrenIdList) {
    if (ids.size() > 0) {
      List<Long> newIdList = getChildIds(ids);
      childrenIdList.addAll(newIdList);
      traverseAllChildren(newIdList, childrenIdList);
    }
  }

  private List<Long> getChildIds(List<Long> ids) {
    return getBaseMapper().selectChildrenIdsInIds(ids);
  }
}
